<?php
namespace Admin\Controller;
use Common\Model\CommonModel;
use Think\Controller;
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/11/7
 * Time: 15:48
 */


class PhotocateController extends BaseController{

    /**
     *
     */
    public function index(){
        $photocateModel=M('photocate');
        //var_dump($photocateModel);exit;
           $condition['name'] = array('like',"%".$_GET['name']."%");
            unset($_SESSION['like']['photocate']);
            $_SESSION['like']['photocate']=$_GET['name'];

        if(!empty($_GET['id'])){
            $condition['pid']=$_GET['id'];
        }else if($_GET['pid']){
            $photocateModel=D('Photocate');
            $cates=$photocateModel->finded('id='.$_GET['pid']);
            $condition['pid']=$cates['pid'];
        }else{
            $condition['pid']=0;
        }
        $count = $photocateModel->where($condition)->count();
        $page= new \Think\Page($count,5);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$photocateModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
        $list=CommonModel::status($list);
        // var_dump($list);exit;
        /*  if(empty($list)){
              $this->
          }*/
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();

    }

    public function create(){
        $photocateModel=D('Photocate');
        if(!empty($_GET)){
            $pid=$_GET['id'];
            $this->assign('pid',$pid);
        }else{
            $list=$photocateModel->getpid();
            $this->assign('cates',$list);
            $this->assign('pid',0);
        }

        $this->display();
    }


    public function doCreate(){
        $photocateModel=D('Photocate');
        $data['pid']=$_POST['pid'];
        $data['name']=$_POST['name'];

        $path=$photocateModel->findPath('id='.$data['pid']);

        //echo $path;exit;

        $data['path']=$path.','.$data['pid'];

        $data['create']=time();
        //  echo $_POST['name'];exit;
        // echo $data['name'];exit;

        $res=$photocateModel->create($data);
        if($res){
            $this->ajaxreturn(1);
        }else{
            $this->ajaxreturn(2);
        }
    }
    //修改页面
    public function update(){
        $photocateModel=D('Photocate');
        //$photocateModel=new PriceModel();
        $da['id']=$_GET['id'];
        //找到原数据
        $list=$photocateModel->finded($da);
        $this->assign('list',$list);
        $this->display();
    }
    //执行修改
    public function doUpdate(){
        $photocateModel=D('Photocate');
        $data['name']=$_POST['name'];
        $id=$_POST['id'];
        $where='id='.$id;
        // echo $id;exit;
        $list=$photocateModel->finded($where);
        /* echo(json_encode($list));
         exit;*/
        $data['update']=time();
        //过滤重复内容
        $data=CommonModel::filter($data,$list);
        // echo json_encode($data);
        // exit;
        $res=$photocateModel->update($where,$data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }
    }

    public function del(){
        $photocateModel=D('Photocate');
        $id=$_GET['id'];
        $list=$photocateModel->finded('pid='.$id);
        if(empty($list)){
            $res= $photocateModel->deleted('id='.$id);
            if($res) {
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('存在子类，不能删除!');
        }
    }
}